// 打开模态框函数
function openPermissionModal() {
  document.getElementById("permissionModal").style.display = "flex";
}

function openEditPermissionModal(permissionName) {
  // 模拟加载权限数据
  document.getElementById("edit-permission-name").value = permissionName;
  document.getElementById("edit-permission-desc").value =
    getPermissionDescription(permissionName);
  document.getElementById("editPermissionModal").style.display = "flex";
}

function openDeletePermissionModal(permissionName) {
  document.getElementById("delete-permission-name").textContent =
    permissionName;
  document.getElementById("deletePermissionModal").style.display = "flex";
}

function openMemberModal() {
  document.getElementById("memberModal").style.display = "flex";
}

function openEditMemberModal(memberName, memberRole) {
  // 模拟加载成员数据
  document.getElementById("edit-member-name").value = "";
  document.getElementById("edit-member-account").value = "";
  document.getElementById("edit-member-password").value = "";
  document.getElementById("edit-member-role").value = "";
  document.getElementById("editMemberModal").style.display = "flex";
}

// 关闭模态框函数
function closeModal(modalId) {
  document.getElementById(modalId).style.display = "none";
}

// 保存函数
function savePermission() {
  alert("权限角色已创建成功！");
  closeModal("permissionModal");
}

function savePermissionChanges() {
  alert("权限角色修改已保存！");
  closeModal("editPermissionModal");
}

function confirmDeletePermission() {
  const permissionName = document.getElementById(
    "delete-permission-name"
  ).textContent;
  alert(`权限角色 "${permissionName}" 已删除！`);
  closeModal("deletePermissionModal");
}

function saveMember() {
  alert("成员已添加成功！");
  closeModal("memberModal");
}

function saveMemberChanges() {
  alert("成员信息修改已保存！");
  closeModal("editMemberModal");
}

function deleteMember() {
  if (confirm("确定要删除此用户吗？此操作不可恢复。")) {
    alert("用户已删除！");
    closeModal("editMemberModal");
  }
}

// 辅助函数
function getPermissionDescription(name) {
  const descriptions = {
    管理员: "拥有所有权限，可以管理系统全部功能",
    访客: "只能查看内容，不能进行任何修改操作",
    协助者: "拥有部分管理权限，可以编辑内容但不能删除",
  };
  return descriptions[name] || "";
}

// 颜色选择
const colorOptions = document.querySelectorAll(".color-option");
colorOptions.forEach((option) => {
  option.addEventListener("click", () => {
    colorOptions.forEach((opt) => opt.classList.remove("selected"));
    option.classList.add("selected");
  });
});

// 点击模态框外部关闭
window.addEventListener("click", (event) => {
  const modals = document.querySelectorAll(".modal");
  modals.forEach((modal) => {
    if (event.target === modal) {
      modal.style.display = "none";
    }
  });
});
